package com.banshuibao.bsb_android.adapter;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AdModel;
import com.banshuibao.bsb_android.model.AuthorizedResultModel;
import com.banshuibao.bsb_android.model.CoachModel;
import com.banshuibao.bsb_android.model.ExpertAnswerDetailModel;
import com.banshuibao.bsb_android.model.ExpertResultModel;
import com.banshuibao.bsb_android.model.LiveRoomModel;
import com.banshuibao.bsb_android.model.MediaModel;
import com.banshuibao.bsb_android.model.OfficeModel;
import com.banshuibao.bsb_android.model.RequestAuthorizedModel;
import com.banshuibao.bsb_android.util.DialogUtils;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.MediaPlayerUtil;
import com.banshuibao.bsb_android.util.ParamsUtil;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.util.QuickDoubleClickable;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.XToast;
import com.banshuibao.bsb_android.view.CenterAlignImageSpan;
import com.banshuibao.bsb_android.view.RedImageView;
import com.cunoraz.gifview.library.GifView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2017/12/15.
 */

public abstract class HomeBaseAdapter extends BaseAdapter {

    private final String isVip;

    public interface HomeOnItemClickListener {

        void goFastQuestion();

        void goCoach(CoachModel.Data item);

        void goAnswer(ExpertResultModel.DataBean dataBean);

        void goLive(String title, String roomId);

        void goMedia(MediaModel.Data data);

        void goOffice(String uuid);

        void goGoods(String uuid);

        void goTag();

        void goInterview(MediaModel.Data interviewData);
    }

    static final int VIEW_TYPE_COACH = 0;

    static final int VIEW_TYPE_ANSWER = 1;

    static final int VIEW_TYPE_LIVE = 2;

    static final int VIEW_TYPE_MEDIA = 3;

    static final int VIEW_TYPE_OFFICE = 4;

    static final int VIEW_TYPE_MORE = 5;

    static final int VIEW_TYPE_TITLE = 6;

    static final int VIEW_TYPE_OFFICE_TITLE = 7;

    static final int VIEW_TYPE_MEDIA_TITLE = 8;

    static final int VIEW_TYPE_OFFICE_GOODS = 9;

    static final int VIEW_TYPE_QUESTION = 10;

    static final int VIEW_TYPE_TITLE_MENU = 11;

    static final int VIEW_TYPE_TAG = 12;

    static final int VIEW_TYPE_EVENT = 13;

    static final int VIEW_TYPE_LINE = 14;

    static final int VIEW_TYPE_COUNT = 15;

    int mediaType = R.id.title1;

    int officeType = R.id.title1;

    private Activity mContext;

    private LayoutInflater inflater;

    private List<DataHolder> dataHolders;

    private List<CoachModel.Data> coachList;

    private List<ExpertResultModel.DataBean> answerList;

    private MediaModel.Data interviewData;

    private List<MediaModel.Data> mediaList;

    private List<MediaModel.Data> eventMediaList;

    private List<MediaModel.Data> classMediaList;

    List<OfficeModel.Data> officeList;

    List<OfficeModel.Data> goodsList;

    private List<AdModel.Data> adList;

    private HomeOnItemClickListener homeOnItemClickListener;

    private MediaPlayerUtil mediaPlayerUtil;

    private GetLoginInfo getLoginInfo;

    public HomeBaseAdapter(Activity context) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        dataHolders = new ArrayList<>();
        mediaPlayerUtil = new MediaPlayerUtil(context);
        mediaPlayerUtil.setOnMediaPlayerPlayingListener(mediaPlayerPlayingListener);
        getLoginInfo = MyApplication.applicationInstance.getLoginInfo();
        isVip = getLoginInfo.getVip();
    }

    public void setHomeOnItemClickListener(HomeOnItemClickListener homeOnItemClickListener) {
        this.homeOnItemClickListener = homeOnItemClickListener;
    }

    public void setCoachList(List<CoachModel.Data> coachList) {
        this.coachList = coachList;
    }

    public void setAnswerList(List<ExpertResultModel.DataBean> answerList) {
        this.answerList = answerList;
    }

    public void setInterviewData(MediaModel.Data interviewData) {
        this.interviewData = interviewData;
    }

    public void setMediaList(List<MediaModel.Data> mediaList) {
        this.mediaList = mediaList;
    }

    public void setClassMediaList(List<MediaModel.Data> classMediaList) {
        this.classMediaList = classMediaList;
    }

    public void setOfficeList(List<OfficeModel.Data> officeList) {
        this.officeList = officeList;
    }

    public void setGoodsList(List<OfficeModel.Data> goodsList) {
        this.goodsList = goodsList;
    }

    public void setAdList(List<AdModel.Data> adList) {
        this.adList = adList;
    }

    public void setEventMediaList(List<MediaModel.Data> eventMediaList) {
        this.eventMediaList = eventMediaList;
    }

    public void notifyHomeBaseAdapter() {
        if (dataHolders != null) {
            dataHolders.clear();
            createAdapterData();
            notifyDataSetChanged();
        }
    }

    abstract void createAdapterData();

    /**
     * 创建政策辅导title
     */
    void createCoachTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("政策解读");
        dataHolders.add(dataHolder);
    }

    /**
     * 创建政策辅导列表
     */
    void createCoachList() {
        if (coachList != null && coachList.size() > 0) {
            int size = coachList.size();
            for (int i = 0; i < size; i++) {
                CoachDataHolder dataHolder = new CoachDataHolder();
                dataHolder.setData(coachList.get(i));
                dataHolders.add(dataHolder);
            }
        }
    }

    /**
     * 创建专家解读title
     */
    void createAnswerTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("专家答疑");
        dataHolders.add(dataHolder);
    }

    /**
     * 创建专家筛选菜单
     */
    void createTag() {
        TagDataHolder dataHolder = new TagDataHolder();
        dataHolders.add(dataHolder);
    }

    /**
     * 创建专家解读列表
     */
    void createAnswerList() {
        if (answerList != null && answerList.size() > 0) {
            int size = answerList.size();
            for (int i = 0; i < size; i++) {
                AnswerDataHolder dataHolder = new AnswerDataHolder();
                dataHolder.setData(answerList.get(i));
                dataHolders.add(dataHolder);
            }
        }
    }

    /**
     * 创建直播title
     */
    void createLiveTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("大咖访谈");
        dataHolders.add(dataHolder);
    }

    /**
     * 创建直播内容
     */
    void createLiveData() {
        if (interviewData != null) {
            LiveDataHolder dataHolder = new LiveDataHolder();
            dataHolders.add(dataHolder);
        }
    }

    /**
     * 创建财税课堂title
     **/
    void createMediaTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("财税课堂");
        //MediaTitleDataHolder dataHolder = new MediaTitleDataHolder();
        dataHolders.add(dataHolder);
    }

    /**
     * 创建财税课堂列表和考前辅导列表
     **/
    void createMediaList() {
        if (mediaList != null && mediaList.size() > 0) {
            MediaDataHolder dataHolder = new MediaDataHolder();
            dataHolder.setData(mediaList);
            dataHolders.add(dataHolder);
        }
       /* List<MediaModel.Data> list = null;
        switch (mediaType) {
            case R.id.title1:
                list = this.mediaList;
                break;
            case R.id.title2:
                list = this.classMediaList;
                break;
        }
        if (list != null && list.size() > 0) {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                MediaDataHolder dataHolder = new MediaDataHolder();
                dataHolder.setData(list.get(i));
                dataHolders.add(dataHolder);
            }
        }*/
    }

    /**
     * 创建技能大赛title
     **/
    void createEventMediaTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("办税技能大赛培训");
        //MediaTitleDataHolder dataHolder = new MediaTitleDataHolder();
        dataHolders.add(dataHolder);
    }

    /**
     * 创建办税技能大赛列表
     **/
    void createEventMediaList() {
        if (eventMediaList != null && eventMediaList.size() > 0) {
            EventDataHolder dataHolder = new EventDataHolder();
            dataHolder.setData(eventMediaList);
            dataHolders.add(dataHolder);
        }
    }

    /**
     * 创建事务所title
     **/
    void createOfficeTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("新闻·动态");
        dataHolders.add(dataHolder);
    }

    /**
     * 创建事务所title
     **/
    void createOfficeGoodsTitle() {
        TitleDataHolder dataHolder = new TitleDataHolder();
        dataHolder.setData("产品·服务");
        dataHolders.add(dataHolder);
    }

    /**
     * 创建事务所内容
     */
    void createOfficeData() {
        if (officeList != null && officeList.size() > 0) {
            int size = officeList.size();
            for (int i = 0; i < size; i++) {
                OfficeDataHolder dataHolder = new OfficeDataHolder();
                dataHolder.setData(officeList.get(i));
                dataHolders.add(dataHolder);
            }
        }
    }

    /**
     * 创建事务所产品
     **/
    void createGoodsData() {
        if (goodsList != null && goodsList.size() > 0) {
            OfficeGoodsDataHolder dataHolder = new OfficeGoodsDataHolder();
            dataHolder.setData(goodsList);
            dataHolders.add(dataHolder);
        }
    }

    /**
     * 创建查看更多内容
     *
     * @param type 属于哪个页面更多
     **/
    void createMore(int type) {
        MoreDataHolder dataHolder = new MoreDataHolder();
        dataHolder.setData(type);
        dataHolders.add(dataHolder);
    }

    /**
     * 创建抢答页面
     * *
     **/
    void createQuestion() {
        QuestionDataHolder dataHolder = new QuestionDataHolder();
        dataHolders.add(dataHolder);
    }

    /**
     * 创建导航条
     **/
    void createTitleMenu() {
        TitleMenuDataHolder dataHolder = new TitleMenuDataHolder();
        dataHolders.add(dataHolder);
    }

    /**
     * 创建line
     */
    void createLineView() {
        LineDataHolder lineDataHolder = new LineDataHolder();
        dataHolders.add(lineDataHolder);
    }


    @Override
    public int getCount() {
        return dataHolders != null ? dataHolders.size() : 0;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        return dataHolders != null ? dataHolders.get(position).getType() : 0;
    }

    @Override
    public Object getItem(int i) {
        return dataHolders != null ? dataHolders.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int type = getItemViewType(i);
        switch (type) {
            case VIEW_TYPE_TITLE_MENU:
                return getTitleMenu(i, view, viewGroup);
            case VIEW_TYPE_TITLE:
                return getTitleView(i, view, viewGroup);
            case VIEW_TYPE_MEDIA_TITLE:
                return getMediaTitleView(i, view, viewGroup);
            case VIEW_TYPE_OFFICE_TITLE:
                return getOfficeTitleView(i, view, viewGroup);
            case VIEW_TYPE_COACH:
                return getCoachView(i, view, viewGroup);
            case VIEW_TYPE_ANSWER:
                return getAnswerView(i, view, viewGroup);
            case VIEW_TYPE_LIVE:
                return getLiveView(i, view, viewGroup);
            case VIEW_TYPE_MEDIA:
                return getMediaView(i, view, viewGroup);
            case VIEW_TYPE_OFFICE:
                return getOfficeView(i, view, viewGroup);
            case VIEW_TYPE_OFFICE_GOODS:
                return getOfficeGoodsView(i, view, viewGroup);
            case VIEW_TYPE_MORE:
                return getMoreView(i, view, viewGroup);
            case VIEW_TYPE_QUESTION:
                return getQuestionView(i, view, viewGroup);
            case VIEW_TYPE_TAG:
                return getTagViewHolder(i, view, viewGroup);
            case VIEW_TYPE_EVENT:
                return getEventView(i, view, viewGroup);
            case VIEW_TYPE_LINE:
                return getLineView(i, view, viewGroup);
        }
        return null;
    }

    private View getTitleMenu(int i, View view, ViewGroup viewGroup) {
        TitleMenuViewHolder holder;
        if (view == null) {
            holder = new TitleMenuViewHolder();
            view = inflater.inflate(R.layout.item_home_title_menu, viewGroup, false);
            holder.coach = view.findViewById(R.id.coach);
            holder.interview = view.findViewById(R.id.interview);
            holder.answer = view.findViewById(R.id.answer);
            holder.media = view.findViewById(R.id.media);
            holder.local = view.findViewById(R.id.local);
            holder.cases = view.findViewById(R.id.cases);
            holder.tools = view.findViewById(R.id.tools);
            holder.class_media = view.findViewById(R.id.class_media);
            holder.rule = view.findViewById(R.id.rule);
            holder.knowledge = view.findViewById(R.id.knowledge);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.coach:
                            IntentManager.goCoachListActivity(mContext);
                            break;
                        case R.id.answer:
                            IntentManager.goExpertsAnswer(mContext);
                            break;
                        case R.id.interview:
                            IntentManager.goInterviewListActivity(mContext);
                            break;
                        case R.id.media:
                            IntentManager.goMediaListActivity(mContext, "media");
                            break;
                        case R.id.local:
                            IntentManager.goAreaIdActivity(mContext);
                            break;
                        case R.id.tools:
                            IntentManager.goToolsActivity(mContext);
                            break;
                        case R.id.cases:
                            IntentManager.goCaseAcitivty(mContext);
                            break;
                        case R.id.knowledge:
                            IntentManager.goKnowledgeAcitivty(mContext);
                            break;
                        case R.id.class_media:
                        case R.id.rule:
                            XToast.showToast(mContext, "敬请期待");
                            break;
                    }
                }
            };
            holder.coach.setOnClickListener(clickListener);
            holder.answer.setOnClickListener(clickListener);
            holder.interview.setOnClickListener(clickListener);
            holder.media.setOnClickListener(clickListener);
            holder.rule.setOnClickListener(clickListener);
            holder.tools.setOnClickListener(clickListener);
            holder.knowledge.setOnClickListener(clickListener);
            holder.local.setOnClickListener(clickListener);
            holder.class_media.setOnClickListener(clickListener);
            holder.cases.setOnClickListener(clickListener);
            view.setTag(holder);
        } else holder = (TitleMenuViewHolder) view.getTag();

        return view;
    }

    private class TitleMenuViewHolder {
        LinearLayout coach, answer, interview, media, class_media, knowledge, cases, tools, local, rule;
    }

    private View getQuestionView(int i, View view, ViewGroup viewGroup) {
        QuestionViewHolder holder;
        if (view == null) {
            holder = new QuestionViewHolder();
            view = inflater.inflate(R.layout.item_home_question, viewGroup, false);
            holder.imageView = view.findViewById(R.id.image);
            view.setTag(holder);
        } else
            holder = (QuestionViewHolder) view.getTag();
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (homeOnItemClickListener != null)
                    homeOnItemClickListener.goFastQuestion();
            }
        });
        return view;
    }

    private class QuestionViewHolder {
        ImageView imageView;
    }

    private View getTitleView(int i, View view, ViewGroup viewGroup) {
        TitleViewHolder holder;
        if (view == null) {
            holder = new TitleViewHolder();
            view = inflater.inflate(R.layout.item_home_title, viewGroup, false);
            holder.title = view.findViewById(R.id.title);
            holder.title.getPaint().setFakeBoldText(true);
            holder.more = view.findViewById(R.id.more);
            view.setTag(holder);
        } else
            holder = (TitleViewHolder) view.getTag();
        TitleDataHolder dataHolder = (TitleDataHolder) getItem(i);
        String title = dataHolder.getData();
        holder.title.setText(title);
        if (TextUtils.equals(title, "大咖访谈")) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_interview_replay);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IntentManager.goInterviewListActivity(mContext);
                }
            });
        } else if (TextUtils.equals(title, "办税技能大赛培训")) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_home_title_more);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goEventListActivity(mContext);
                }
            });
        } else if (TextUtils.equals("财税课堂", title)) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_home_title_more);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goMediaListActivity(mContext, "media");
                }
            });
        } else if (TextUtils.equals("专家答疑", title)) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_home_title_more);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goExpertsAnswer(mContext);
                }
            });
        } else if (TextUtils.equals("政策解读", title)) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_home_title_more);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goCoachListActivity(mContext);
                }
            });
        } else if (TextUtils.equals("新闻·动态", title)) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_home_title_more);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goOfficeListActivity(mContext);
                }
            });
        } else if (TextUtils.equals("产品·服务", title)) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setImageResource(R.drawable.icon_home_title_more);
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goOfficeGoodsListActivity(mContext);
                }
            });
        } else {
            holder.more.setVisibility(View.GONE);
            holder.more.setImageResource(0);
            holder.more.setOnClickListener(null);
        }
        return view;
    }

    private class TitleViewHolder {
        TextView title;
        ImageView record_img;
        RedImageView answer_img;
        ImageView more;
    }

    private View getMediaTitleView(int i, View view, ViewGroup viewGroup) {
        DoubleTitleViewHolder holder;
        if (view == null) {
            holder = new DoubleTitleViewHolder();
            view = inflater.inflate(R.layout.item_home_double_btn_title, viewGroup, false);
            holder.title1 = view.findViewById(R.id.title1);
            holder.title2 = view.findViewById(R.id.title2);
            holder.title1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaType == R.id.title1)
                        return;
                    mediaType = R.id.title1;
                    notifyHomeBaseAdapter();
                }
            });
            holder.title2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaType == R.id.title2)
                        return;
                    mediaType = R.id.title2;
                    notifyHomeBaseAdapter();
                }
            });
            view.setTag(holder);
        } else
            holder = (DoubleTitleViewHolder) view.getTag();
        holder.title1.setText("财税微课堂");
        holder.title2.setText("考前辅导");
        return view;
    }

    private View getOfficeTitleView(int i, View view, ViewGroup viewGroup) {
        DoubleTitleViewHolder holder;
        if (view == null) {
            holder = new DoubleTitleViewHolder();
            view = inflater.inflate(R.layout.item_home_double_btn_title, viewGroup, false);
            holder.more = view.findViewById(R.id.more);
            holder.title1 = view.findViewById(R.id.title1);
            holder.title2 = view.findViewById(R.id.title2);
            holder.title1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (officeType == R.id.title1)
                        return;
                    officeType = R.id.title1;
                    notifyHomeBaseAdapter();
                }
            });
            holder.title2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (officeType == R.id.title2)
                        return;
                    officeType = R.id.title2;
                    notifyHomeBaseAdapter();
                }
            });
            view.setTag(holder);
        } else
            holder = (DoubleTitleViewHolder) view.getTag();
        holder.title1.setText("事务所动态");
        holder.title2.setText("核心产品");
        if (GetLoginInfo.getLoginInfoInstance(mContext).readUser().getRole().equals("0")) {
            holder.more.setVisibility(View.VISIBLE);
            holder.more.setText("咨询我所");
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IntentManager.goConsultingOfficeActivity(mContext);
                }
            });
        } else holder.more.setVisibility(View.GONE);
        //holder.more.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_advice, 0, 0, 0);
        return view;
    }

    private class DoubleTitleViewHolder {
        RadioButton title1, title2;
        TextView more;
    }

    private View getCoachView(int i, View view, ViewGroup viewGroup) {
        CoachViewHolder holder;
        if (view == null) {
            holder = new CoachViewHolder();
            view = inflater.inflate(R.layout.item_coach_list, viewGroup, false);
            holder.title = view.findViewById(R.id.title);
            holder.btn = view.findViewById(R.id.btn);
            holder.time = view.findViewById(R.id.time);
            view.setTag(holder);
        } else
            holder = (CoachViewHolder) view.getTag();

        CoachDataHolder dataHolder = (CoachDataHolder) getItem(i);
        final CoachModel.Data item = dataHolder.getData();
        if (item != null) {
            holder.title.setText(item.getTitle());
//            holder.title.getPaint().setFakeBoldText(true);
            holder.time.setText(TimeUtil.format(item.getCreateDate()));
            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goCoachInterpretationActivity(mContext, item.getUuid());
                }
            });
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (homeOnItemClickListener != null)
                        homeOnItemClickListener.goCoach(item);
                }
            });
        }
        return view;
    }

    private class CoachViewHolder {
        TextView title, time;
        ImageView btn;
    }

    private View getAnswerView(int i, View view, ViewGroup viewGroup) {
        AnswerViewHolder holder;
        if (view == null) {
            holder = new AnswerViewHolder();
            view = inflater.inflate(R.layout.item_home_answer, viewGroup, false);
            holder.title = view.findViewById(R.id.title);
            holder.name = view.findViewById(R.id.name);
            holder.skill = view.findViewById(R.id.skill);
            holder.time = view.findViewById(R.id.time);
            holder.header = view.findViewById(R.id.header);
            holder.buy = view.findViewById(R.id.buy);
            holder.buy_tag = view.findViewById(R.id.buy_tag);
            holder.buy_price = view.findViewById(R.id.buy_price);
            holder.count = view.findViewById(R.id.count);
            //  holder.audio_icon = view.findViewById(R.id.audio_icon);
            holder.more = view.findViewById(R.id.more);
            view.setTag(holder);
        } else
            holder = (AnswerViewHolder) view.getTag();
        AnswerDataHolder dataHolder = (AnswerDataHolder) getItem(i);
        final ExpertResultModel.DataBean dataBean = dataHolder.getData();
        if (dataBean != null) {
            holder.name.setText(dataBean.getName());
            if (!TextUtils.isEmpty(dataBean.getDuty())) {
                holder.skill.setText(dataBean.getDuty());
            } else {
                holder.skill.setText(dataBean.getPositiotn());
            }
            holder.title.setText(dataBean.getQuestion());
            //holder.audio_icon.setText("点击进详情");
            holder.time.setText(TimeUtil.format(dataBean.getCreateDate()));
            holder.count.setText("听过:" + dataBean.getBuyNum() + "人");
            if (dataBean.getExpertVoices() != null && dataBean.getExpertVoices().size() > 0) {
                setMediaView(view, dataBean.getExpertVoices(), dataBean);
            }

            if (dataBean.getExpertVoices() != null && dataBean.getExpertVoices().size() > 2)
                holder.more.setVisibility(View.VISIBLE);
            else
                holder.more.setVisibility(View.GONE);
            ImageUtil.loadImageHead(mContext, dataBean.getHeadImg(), holder.header);
            if (dataBean.getQuPrice() == 0) {
                holder.buy_price.setVisibility(View.GONE);
                holder.buy.setTextColor(mContext.getResources().getColor(R.color.free_price));
                holder.buy.setText("限时免费听");
            } else {
                holder.buy_price.setVisibility(View.VISIBLE);
                holder.buy.setTextColor(mContext.getResources().getColor(R.color.charge_price));
                if (isVip.equals("0")) {
                    holder.buy_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                    holder.buy.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                    holder.buy.setText(PriceUtil.FormatDecimal(dataBean.getQuPrice()));
                    holder.buy_tag.setVisibility(View.VISIBLE);
                } else {
                    holder.buy_tag.setVisibility(View.GONE);
                    holder.buy_price.getPaint().setFlags(0);
                    holder.buy.getPaint().setFlags(0);
                    holder.buy_price.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                    holder.buy.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                    holder.buy.setText(PriceUtil.FormatDecimal(dataBean.getQuPrice()));
                }
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (homeOnItemClickListener != null)
                        homeOnItemClickListener.goAnswer(dataBean);
                }
            });
            holder.header.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IntentManager.goPersonPageActivity(mContext, dataBean.getUserId());
                }
            });
        }
        return view;
    }

    private String voiceName, voiceContent;
    private MediaPlayer media;
    private String time;

    private void setMediaView(View view, List<ExpertAnswerDetailModel.DataBean.VoicesBean> datas, ExpertResultModel.DataBean dataBean) {
        final ExpertAnswerDetailModel.DataBean.VoicesBean voicesBean = datas.get(0);
        final VoiceViewHolder holder = new VoiceViewHolder();
        holder.text_tv = view.findViewById(R.id.text_tv);
        holder.show_tv = view.findViewById(R.id.show_tv);
        holder.voice_iv = view.findViewById(R.id.voice_iv);
        holder.voice_gif = view.findViewById(R.id.voice_gif);
        holder.mediaLayout1 = view.findViewById(R.id.media_layout1);
        holder.play = view.findViewById(R.id.play);
        setMediaData(holder, voicesBean, dataBean);


        VoiceViewHolder holder1 = new VoiceViewHolder();
        holder1.text_tv = view.findViewById(R.id.text_tv2);
        holder1.show_tv = view.findViewById(R.id.show_tv2);
        holder1.voice_iv = view.findViewById(R.id.voice_iv2);
        holder1.voice_gif = view.findViewById(R.id.voice_gif2);
        holder1.mediaLayout1 = view.findViewById(R.id.media_layout2);
        holder1.play = view.findViewById(R.id.play2);
        holder1.mediaLayout1.setVisibility(View.VISIBLE);
        holder.show_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        holder.show_tv.getPaint().setAntiAlias(true);//抗锯齿
        holder1.show_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        holder1.show_tv.getPaint().setAntiAlias(true);//抗锯齿
        if (datas.size() > 1) {
            final ExpertAnswerDetailModel.DataBean.VoicesBean voicesBean1 = datas.get(1);
            holder1.mediaLayout1.setVisibility(View.VISIBLE);
            setMediaData(holder1, voicesBean1, dataBean);
        } else
            holder1.mediaLayout1.setVisibility(View.GONE);
    }

    private void setMediaData(final VoiceViewHolder holder, final ExpertAnswerDetailModel.DataBean.VoicesBean voicesBean, final ExpertResultModel.DataBean dataBean) {
        holder.voice_gif.setGifResource(R.raw.play);
        holder.text_tv.setText(TimeUtil.formatDuring(voicesBean.getVoiceTime()));
        holder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //审请权限
                getAuthorized(dataBean.getUuid(), voicesBean.getUuid(), TimeUtil.formatDuring(voicesBean.getVoiceTime()), holder, "voice", dataBean);
            }
        });
        holder.show_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //审请权限
                getAuthorized(dataBean.getUuid(), voicesBean.getUuid(), TimeUtil.formatDuring(voicesBean.getVoiceTime()), holder, "text", dataBean);
            }
        });
        if (TextUtils.equals(voicesBean.getUuid(), playUuid)) {
            holder.play.setBackground(mContext.getResources().getDrawable(R.drawable.listen_foucu));
        } else {
            holder.play.setBackground(mContext.getResources().getDrawable(R.drawable.listenback));
        }
    }

    private Handler handler = new Handler() {
        VoiceViewHolder holder;
        private boolean stop = false;

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    if (!stop && holder != null && media != null) {
                        sendEmptyMessageDelayed(0, 1000);
                        holder.text_tv.setText(ParamsUtil.millsecondsToStr(media.getDuration() - media.getCurrentPosition()));
                        holder.voice_iv.setVisibility(View.GONE);
                        holder.voice_gif.setVisibility(View.VISIBLE);
                    }
                    break;
                case 1:
                    holder = (VoiceViewHolder) msg.obj;
                    stop = false;
                    holder.play.setBackground(mContext.getResources().getDrawable(R.drawable.listen_foucu));
                    break;
                case 2:
                    if (holder == null || media == null)
                        return;
                    stop = true;
                    playUuid = "";
                    holder.voice_iv.setVisibility(View.VISIBLE);
                    holder.voice_gif.setVisibility(View.GONE);
                    holder.play.setBackground(mContext.getResources().getDrawable(R.drawable.listenback));
                    holder.text_tv.setText(time);
                    media.seekTo(0);
                    media.pause();
                    media.stop();
                    media.reset();
                    media = null;
                    break;
                case 3:
                    if (media != null) {
                        if (media.isPlaying()) {
                            stop = true;
                            media.pause();
                            holder.voice_iv.setVisibility(View.VISIBLE);
                            holder.voice_gif.setVisibility(View.GONE);
                        } else {
                            stop = false;
                            sendEmptyMessageDelayed(0, 1000);
                            media.start();
                        }
                    }
                    break;
            }
        }
    };

    MediaPlayerUtil.MediaPlayerPlayingListener mediaPlayerPlayingListener = new MediaPlayerUtil.MediaPlayerPlayingListener() {
        @Override
        public void OnError(MediaPlayer mediaPlayer) {

        }

        @Override
        public void OnPrepared(MediaPlayer mediaPlayer) {
            //开始播放
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }

        @Override
        public void OnCompletion(MediaPlayer mediaPlayer) {
            stopMedia();
        }
    };

    private String playUuid;

    /**
     * 调用援权接口是否可听可看
     */
    private void getAuthorized(String questionUuid, final String voiceUuid, final String during, final VoiceViewHolder holder, final String tag, final ExpertResultModel.DataBean dataBean) {
        if (QuickDoubleClickable.isDoubleClick()) {
            return;
        }
        RequestAuthorizedModel requestAuthorizedModel = new RequestAuthorizedModel();
        requestAuthorizedModel.setUuid(questionUuid);
        requestAuthorizedModel.setVoiceId(voiceUuid);
        HttpUtil.doPost(Constants.Url.AUDIO_AUTHORIZED, requestAuthorizedModel, new HttpResponse(mContext, AuthorizedResultModel.class) {
            @Override
            public void onResult(Object obj) {
                AuthorizedResultModel resultModel = (AuthorizedResultModel) obj;
                AuthorizedResultModel.DataBean data = resultModel.getData();
                if (data != null) {
                    String type = data.getType();
                    if ("0".equals(type)) {
                        voiceName = data.getVoiceName();
                        voiceContent = data.getVoiceText();
                        if ("voice".equals(tag)) {
                            if (media != null) {
                                if (voiceUuid.equals(playUuid)) {
                                    handler.sendEmptyMessage(3);
                                } else {
                                    handler.sendEmptyMessage(2);
                                }
                            } else {
                                playUuid = voiceUuid;
                                time = String.valueOf(during);
                                Message msg = Message.obtain();
                                msg.what = 1;
                                msg.obj = holder;
                                handler.sendMessage(msg);
                                playAudio(voiceName);
                            }
                        } else if ("text".equals(tag)) {
                            showText(voiceContent);
                        }

                    } else if ("1".equals(type)) {
                        voiceName = "";
                        voiceContent = "";
                        showBuy(dataBean);
                    }
                }
            }
        });
    }

    /**
     * 播放语音
     */
    private void playAudio(String name) {
        media = mediaPlayerUtil.getNetMedia(Constants.Url.AUDIO_PLAY + name);
    }

    public void showText(String content) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setCancelable(false);  //先得到构造器
        builder.setMessage(content); //设置内容
        builder.setNegativeButton("已阅", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        //参数都设置完成了，创建并显示出来
        builder.create().show();
    }

    /**
     * 是否购买
     */
    private void showBuy(final ExpertResultModel.DataBean dataBean) {
        Dialog buyDialog = DialogUtils.payDialog(mContext, "您可以支付" + String.valueOf(dataBean.getQuPrice()) + "元购买单次或支付365元购买会员卡收听所有语音!",
                dataBean.getUuid(), String.valueOf(dataBean.getQuPrice()), dataBean.getQuestion(), 5);
        if (!buyDialog.isShowing())
            buyDialog.show();
//        AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setCancelable(false);  //先得到构造器
//        builder.setTitle("提示"); //设置标题
//        builder.setMessage("收听/查看此问题需要支付" + dataBean.getQuPrice() + "元"); //设置内容
//        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { //设置确定按钮
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                IntentManager.goPayResultActivity(mContext, dataBean.getQuestion(), String.valueOf(dataBean.getQuPrice()), dataBean.getUuid(), 5);
//            }
//        });
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
//        //参数都设置完成了，创建并显示出来
//        builder.create().show();
    }


    public void stopMedia() {
        handler.sendEmptyMessage(2);
    }

    private class VoiceViewHolder {
        LinearLayout mediaLayout1, play;
        TextView text_tv, show_tv;
        ImageView voice_iv;
        GifView voice_gif;
    }

    private class AnswerViewHolder {
        TextView title, name, skill, time, buy, audio_icon, count , buy_tag,buy_price;
        ImageView header, more;
    }

    private View getLiveView(int i, View view, ViewGroup viewGroup) {
        LiveViewHolder holder;
        if (view == null) {
            holder = new LiveViewHolder();
            view = inflater.inflate(R.layout.item_home_live, viewGroup, false);
            holder.video1 = view.findViewById(R.id.video1);
            holder.video_name_1 = view.findViewById(R.id.videoName1);
            // holder.video_status_1 = view.findViewById(R.id.video_status_1);
            holder.video2 = view.findViewById(R.id.video2);
            holder.video_name_2 = view.findViewById(R.id.videoName2);
            holder.video_status_2 = view.findViewById(R.id.video_status_2);
            holder.video1_img = view.findViewById(R.id.video1_img);
            holder.video2_img = view.findViewById(R.id.video2_img);
            holder.live1_bg = view.findViewById(R.id.live1_bg);
            holder.live2_bg = view.findViewById(R.id.live2_bg);
            //holder.interview_name = view.findViewById(R.id.interview_name);
            view.setTag(holder);
        } else
            holder = (LiveViewHolder) view.getTag();
        if (interviewData != null) {
            //holder.live1_bg.setTag(liveData);
            holder.video_name_1.setText(interviewData.getTitle());
            ImageUtil.loadImage(mContext, interviewData.getImage(), holder.video1_img);
           /* if (liveData.getStatus() == 1) {
                holder.video_status_1.setText("直播中");
                holder.video_status_1.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.live_status_icon, 0, 0, 0);
            } else {
                holder.video_status_1.setText("直播未开始");
                holder.video_status_1.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.live_status_no_icon, 0, 0, 0);
            }*/
            holder.video1_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  /*  if (liveData.getStatus() != 1)
                        XToast.showToast(mContext, "直播未开始");
                    else if (homeOnItemClickListener != null)*//*
                        homeOnItemClickListener.goLive(liveData.getName(), liveData.getRoomid());*/
                    if (homeOnItemClickListener != null)
                        homeOnItemClickListener.goInterview(interviewData);
                }
            });
        }
        if (adList != null && adList.size() > 0) {
            final AdModel.Data ad = adList.get(0);
            ImageUtil.loadImage(mContext, ad.getImage(), holder.video2_img);
            holder.video_name_2.setText(ad.getTitle());
            holder.video2_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IntentManager.goWebViewActivity(mContext, ad.getTitle(), ad.getId(), "live", "2".equals(ad.getIsNative()));
                }
            });
        }
        return view;
    }

    private SpannableString linkBgColor(String txt, int startIndex,
                                        int color, int endIndex) {
        SpannableString spannableString = new SpannableString(txt);
        Drawable drawable = mContext.getResources().
                getDrawable(R.drawable.icon_home_announce);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        spannableString.setSpan(new CenterAlignImageSpan(drawable),
                startIndex,
                endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
       /* spannableString.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.white)),
                startIndex,
                endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new BackgroundColorSpan(color), startIndex,
                endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(20), startIndex,
                endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );*/
        return spannableString;
    }

    private class LiveViewHolder {

        private RelativeLayout video1, video2;

        private TextView video_name_1, video_name_2, video_status_1, video_status_2, interview_name;

        private ImageView video1_img, video2_img;

        private View live1_bg, live2_bg;
    }

    private View getMediaView(int i, View view, ViewGroup viewGroup) {
        MediaViewHolder holder;
        if (view == null) {
            holder = new MediaViewHolder();
            view = inflater.inflate(R.layout.item_home_media, viewGroup, false);
            holder.img1 = view.findViewById(R.id.img1);
            holder.img2 = view.findViewById(R.id.img2);
            holder.img3 = view.findViewById(R.id.img3);
            holder.img4 = view.findViewById(R.id.img4);
            holder.media1 = view.findViewById(R.id.media1);
            holder.media2 = view.findViewById(R.id.media2);
            holder.media3 = view.findViewById(R.id.media3);
            holder.media4 = view.findViewById(R.id.media4);
            holder.text1 = view.findViewById(R.id.text1);
            holder.text2 = view.findViewById(R.id.text2);
            holder.text3 = view.findViewById(R.id.text3);
            holder.text4 = view.findViewById(R.id.text4);
            holder.img1 = view.findViewById(R.id.img1);
            holder.img2 = view.findViewById(R.id.img2);
            holder.img3 = view.findViewById(R.id.img3);
            holder.img4 = view.findViewById(R.id.img4);
            holder.price1 = view.findViewById(R.id.price1);
            holder.price2 = view.findViewById(R.id.price2);
            holder.price3 = view.findViewById(R.id.price3);
            holder.price4 = view.findViewById(R.id.price4);
            holder.price_icon1 = view.findViewById(R.id.price_icon);
            holder.price_icon2 = view.findViewById(R.id.price_icon2);
            holder.price_icon3 = view.findViewById(R.id.price_icon3);
            holder.price_icon4 = view.findViewById(R.id.price_icon4);
            holder.price1.getPaint().setFakeBoldText(true);
            holder.price_icon1.getPaint().setFakeBoldText(true);
            holder.price2.getPaint().setFakeBoldText(true);
            holder.price_icon2.getPaint().setFakeBoldText(true);
            holder.price3.getPaint().setFakeBoldText(true);
            holder.price_icon3.getPaint().setFakeBoldText(true);
            holder.price4.getPaint().setFakeBoldText(true);
            holder.price_icon4.getPaint().setFakeBoldText(true);
            holder.vip1 = view.findViewById(R.id.vip1);
            holder.vip2 = view.findViewById(R.id.vip2);
            holder.vip3 = view.findViewById(R.id.vip3);
            holder.vip4 = view.findViewById(R.id.vip4);
            view.setTag(holder);
        } else
            holder = (MediaViewHolder) view.getTag();
        MediaDataHolder dataHolder = (MediaDataHolder) getItem(i);
        List<MediaModel.Data> datas = dataHolder.getData();
        if (datas != null && datas.size() > 0) {
            int size = datas.size();
            for (int j = 0; j < size; ++j) {
                switch (j) {
                    case 0:
                        setMediaData(holder.text1, holder.img1, holder.price1, holder.media1, holder.price_icon1, holder.vip1, datas.get(0));
                        break;
                    case 1:
                        setMediaData(holder.text2, holder.img2, holder.price2, holder.media2, holder.price_icon2, holder.vip2, datas.get(1));
                        break;
                    case 2:
                        setMediaData(holder.text3, holder.img3, holder.price3, holder.media3, holder.price_icon3, holder.vip3, datas.get(2));
                        break;
                    case 3:
                        setMediaData(holder.text4, holder.img4, holder.price4, holder.media4, holder.price_icon4, holder.vip4, datas.get(3));
                        break;
                }
            }
        }
        return view;
    }

    private void setMediaData(TextView text, ImageView img, TextView price, LinearLayout linearLayout, TextView price_icon, TextView vip, final MediaModel.Data data) {
        text.setText(data.getTitle());
        if (data.getPrice() == 0) {
            vip.setVisibility(View.GONE);
            price_icon.getPaint().setFlags(0);
            price.getPaint().setFlags(0);
            price_icon.setVisibility(View.GONE);
            price.setText("限时免费");
            price.setTextColor(mContext.getResources().getColor(R.color.free_price));
        } else {
            price_icon.setVisibility(View.VISIBLE);
            price.setText(PriceUtil.FormatDecimal(data.getPrice()));
            price.setTextColor(mContext.getResources().getColor(R.color.charge_price));
            if (getLoginInfo != null && getLoginInfo.getVip().equals("0")) {
                price_icon.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                vip.setVisibility(View.VISIBLE);
            } else {
                price_icon.getPaint().setFlags(0);
                price.getPaint().setFlags(0);
                vip.setVisibility(View.GONE);
            }
        }
        price.getPaint().setAntiAlias(true);
        price_icon.getPaint().setAntiAlias(true);
        ImageUtil.loadImage(mContext, data.getImage(), img);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (homeOnItemClickListener != null)
                    homeOnItemClickListener.goMedia(data);
            }
        });
    }

    private class MediaViewHolder {
     /*   ImageView ad_iv;
        TextView title, price, new_icon;*/

        LinearLayout media1, media2, media3, media4;
        TextView text1, text2, text3, text4, price1, price2, price3, price4,
                price_icon1, price_icon2, price_icon3, price_icon4, vip1,
                vip2, vip3, vip4;
        ImageView img1, img2, img3, img4;
    }

    private View getEventView(int i, View view, ViewGroup viewGroup) {
        EventViewHolder holder;
        if (view == null) {
            holder = new EventViewHolder();
            view = inflater.inflate(R.layout.item_home_event_media, viewGroup, false);
            holder.img1 = view.findViewById(R.id.img1);
            holder.img2 = view.findViewById(R.id.img2);
            holder.media1 = view.findViewById(R.id.media1);
            holder.media2 = view.findViewById(R.id.media2);
            holder.text1 = view.findViewById(R.id.text1);
            holder.text2 = view.findViewById(R.id.text2);
            holder.img1 = view.findViewById(R.id.img1);
            holder.img2 = view.findViewById(R.id.img2);
            view.setTag(holder);
        } else
            holder = (EventViewHolder) view.getTag();
        EventDataHolder dataHolder = (EventDataHolder) getItem(i);
        List<MediaModel.Data> datas = dataHolder.getData();
        if (datas != null && datas.size() > 0) {
            int size = datas.size();
            for (int j = 0; j < size; ++j) {
                switch (j) {
                    case 0:
                        setEventData(holder.text1, holder.img1, holder.media1, datas.get(0));
                        break;
                    case 1:
                        setEventData(holder.text2, holder.img2, holder.media2, datas.get(1));
                        break;
                }
            }
        }
        return view;
    }

    private void setEventData(TextView text, ImageView img, LinearLayout linearLayout, final MediaModel.Data data) {
        text.setText(data.getTitle());
        ImageUtil.loadImage(mContext, data.getImage(), img);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (homeOnItemClickListener != null)
                    homeOnItemClickListener.goMedia(data);
            }
        });
    }

    private class EventViewHolder {
        LinearLayout media1, media2;
        TextView text1, text2;
        ImageView img1, img2;
    }

    private View getOfficeView(int i, View view, ViewGroup viewGroup) {
        OfficeViewHolder holder;
        if (view == null) {
            holder = new OfficeViewHolder();
            view = inflater.inflate(R.layout.item_home_office, viewGroup, false);
            holder.title = view.findViewById(R.id.title);
            holder.tag = view.findViewById(R.id.tag);
            view.setTag(holder);
        } else
            holder = (OfficeViewHolder) view.getTag();
        OfficeDataHolder dataHolder = (OfficeDataHolder) getItem(i);
        final OfficeModel.Data item = dataHolder.getData();
        if (item != null) {
            holder.title.setText(item.getTitle());
            holder.tag.setText(item.getLable());
            GradientDrawable gradientDrawable = (GradientDrawable) holder.tag.getBackground();
            if ("动态".equals(item.getLable())) {
                gradientDrawable.setColor(mContext.getResources().getColor(R.color.d_t_color));
            } else if ("快讯".equals(item.getLable())) {
                gradientDrawable.setColor(mContext.getResources().getColor(R.color.k_x_color));
            } else if ("活动".equals(item.getLable())) {
                gradientDrawable.setColor(mContext.getResources().getColor(R.color.h_d_color));
            } else if ("头条".equals(item.getLable())) {
                gradientDrawable.setColor(mContext.getResources().getColor(R.color.t_t_color));
            } else {
                gradientDrawable.setColor(mContext.getResources().getColor(R.color.white));
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (homeOnItemClickListener != null)
                        homeOnItemClickListener.goOffice(item.getUuid());
                }
            });
        }
        return view;
    }

    private class OfficeViewHolder {
        TextView title, time, tag;
    }

    private View getOfficeGoodsView(int i, View view, ViewGroup viewGroup) {
        OfficeGoodsViewHolder holder;
        if (view == null) {
            holder = new OfficeGoodsViewHolder();
            view = inflater.inflate(R.layout.item_home_office_goods, viewGroup, false);
            holder.img1 = view.findViewById(R.id.img1);
            holder.img2 = view.findViewById(R.id.img2);
            holder.img3 = view.findViewById(R.id.img3);
            holder.img4 = view.findViewById(R.id.img4);
            holder.media1 = view.findViewById(R.id.media1);
            holder.media2 = view.findViewById(R.id.media2);
            holder.media3 = view.findViewById(R.id.media3);
            holder.media4 = view.findViewById(R.id.media4);
            holder.text1 = view.findViewById(R.id.text1);
            holder.text2 = view.findViewById(R.id.text2);
            holder.text3 = view.findViewById(R.id.text3);
            holder.text4 = view.findViewById(R.id.text4);
            holder.img1 = view.findViewById(R.id.img1);
            holder.img2 = view.findViewById(R.id.img2);
            holder.img3 = view.findViewById(R.id.img3);
            holder.img4 = view.findViewById(R.id.img4);
            holder.price1 = view.findViewById(R.id.price1);
            holder.price2 = view.findViewById(R.id.price2);
            holder.price3 = view.findViewById(R.id.price3);
            holder.price4 = view.findViewById(R.id.price4);
            holder.price_icon1 = view.findViewById(R.id.price_icon);
            holder.price_icon2 = view.findViewById(R.id.price_icon2);
            holder.price_icon3 = view.findViewById(R.id.price_icon3);
            holder.price_icon4 = view.findViewById(R.id.price_icon4);
            holder.layout1 = view.findViewById(R.id.layout1);
            holder.layout2 = view.findViewById(R.id.layout2);
            holder.price1.getPaint().setFakeBoldText(true);
            holder.price_icon1.getPaint().setFakeBoldText(true);
            holder.price2.getPaint().setFakeBoldText(true);
            holder.price_icon2.getPaint().setFakeBoldText(true);
            holder.price3.getPaint().setFakeBoldText(true);
            holder.price_icon3.getPaint().setFakeBoldText(true);
            holder.price4.getPaint().setFakeBoldText(true);
            holder.price_icon4.getPaint().setFakeBoldText(true);
            view.setTag(holder);
        } else
            holder = (OfficeGoodsViewHolder) view.getTag();
        OfficeGoodsDataHolder dataHolder = (OfficeGoodsDataHolder) getItem(i);
        final List<OfficeModel.Data> list = dataHolder.getData();
        if (list != null && list.size() > 0) {
            int size = list.size();
            for (int j = 0; j < size; ++j) {
                final OfficeModel.Data item = list.get(j);
                String price = item.getPrice() != 0 ? String.valueOf(item.getPrice()) : "面议";
                switch (j) {
                    case 0:
                        setGoodsVisibility(holder, View.VISIBLE, View.INVISIBLE, View.GONE, View.GONE);
                        setGoodsData(holder.text1, holder.price1, holder.img1, holder.media1, item);
                        break;
                    case 1:
                        setGoodsVisibility(holder, View.VISIBLE, View.VISIBLE, View.GONE, View.GONE);
                        setGoodsData(holder.text2, holder.price2, holder.img2, holder.media2, item);
                        break;
                    case 2:
                        setGoodsVisibility(holder, View.VISIBLE, View.VISIBLE, View.VISIBLE, View.INVISIBLE);
                        setGoodsData(holder.text3, holder.price3, holder.img3, holder.media3, item);
                        break;
                    case 3:
                        setGoodsVisibility(holder, View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE);
                        setGoodsData(holder.text4, holder.price4, holder.img4, holder.media4, item);
                        break;
                }

            }
        } else {
            setGoodsVisibility(holder, View.GONE, View.GONE, View.GONE, View.GONE);
        }
        return view;
    }

    private void setGoodsVisibility(OfficeGoodsViewHolder holder, int... visibilities) {
        for (int i = 0; i < visibilities.length; ++i)
            switch (i) {
                case 0:
                    holder.layout2.setVisibility(View.GONE);
                    holder.media1.setVisibility(visibilities[i]);
                    break;
                case 1:
                    holder.layout2.setVisibility(View.GONE);
                    holder.media2.setVisibility(visibilities[i]);
                    break;
                case 2:
                    holder.layout2.setVisibility(View.VISIBLE);
                    holder.media3.setVisibility(visibilities[i]);
                    break;
                case 3:
                    holder.layout2.setVisibility(View.VISIBLE);
                    holder.media4.setVisibility(visibilities[i]);
                    break;
            }

    }

    private void setGoodsData(TextView title, TextView price, ImageView image, View layout, final OfficeModel.Data item) {
        title.setText(item.getTitle());
        if (item.getPrice() == 0) {
            price.setTextSize(12);
            price.setText("面议");
        } else {
            price.setTextSize(13);
            price.setText(PriceUtil.FormatDecimal(item.getPrice()));
        }
        ImageUtil.loadRoundImage(mContext, item.getLogImg(), image, 20, 0);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (homeOnItemClickListener != null)
                    homeOnItemClickListener.goGoods(item.getUuid());
            }
        });
    }

    private class OfficeGoodsViewHolder {
        LinearLayout media1, media2, media3, media4, layout1, layout2;
        TextView text1, text2, text3, text4, price1, price2, price3, price4,
                price_icon1, price_icon2, price_icon3, price_icon4;
        ImageView img1, img2, img3, img4;
    }

    private View getMoreView(int i, View view, ViewGroup viewGroup) {
        MoreViewHolder holder;
        if (view == null) {
            holder = new MoreViewHolder();
            view = inflater.inflate(R.layout.item_home_more, viewGroup, false);
            holder.more = view.findViewById(R.id.more);
            view.setTag(holder);
        } else
            holder = (MoreViewHolder) view.getTag();
        MoreDataHolder dataHolder = (MoreDataHolder) getItem(i);
        final int type = dataHolder.getData();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type) {
                    case VIEW_TYPE_COACH:
                        IntentManager.goCoachListActivity(mContext);
                        break;
                    case VIEW_TYPE_ANSWER:
                        IntentManager.goExpertsAnswer(mContext);
                        break;
                    case VIEW_TYPE_MEDIA:
                        IntentManager.goMediaListActivity(mContext, "media");
                        break;
                    case VIEW_TYPE_OFFICE:
                        IntentManager.goOfficeListActivity(mContext);
                        break;
                }
            }
        });
        return view;
    }

    private class MoreViewHolder {
        TextView more;
    }

    private class TagViewHolder {
        TextView trade;
        TextView tax;
        TextView topic;
    }

    private View getTagViewHolder(int i, View view, ViewGroup viewGroup) {
        TagViewHolder tagViewHolder;
        if (view == null) {
            tagViewHolder = new TagViewHolder();
            view = inflater.inflate(R.layout.tag_show_layout, viewGroup, false);
            tagViewHolder.trade = view.findViewById(R.id.trade);
            tagViewHolder.tax = view.findViewById(R.id.tax);
            tagViewHolder.topic = view.findViewById(R.id.topic);
            view.setTag(tagViewHolder);
        } else {
            tagViewHolder = (TagViewHolder) view.getTag();
        }
        tagViewHolder.trade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentManager.goMenuShowActivity(mContext, "trade");
            }
        });
        tagViewHolder.tax.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentManager.goMenuShowActivity(mContext, "tax");
            }
        });
        tagViewHolder.topic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentManager.goMenuShowActivity(mContext, "topic");
            }
        });
        return view;
    }


    private View getLineView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = new View(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    mContext.getResources().getDimensionPixelSize(R.dimen.padding_10));
            view.setLayoutParams(params);
            view.setBackgroundColor(mContext.getResources().getColor(R.color.line_color));
        }
        return view;
    }

    private abstract class DataHolder<T> {
        private T data;
        private int type;

        public void setData(T data) {
            this.data = data;
        }

        public T getData() {
            return data;
        }

        abstract int getType();
    }

    private class CoachDataHolder extends DataHolder<CoachModel.Data> {

        @Override
        int getType() {
            return VIEW_TYPE_COACH;
        }
    }

    private class AnswerDataHolder extends DataHolder<ExpertResultModel.DataBean> {

        @Override
        int getType() {
            return VIEW_TYPE_ANSWER;
        }
    }

    private class LiveDataHolder extends DataHolder<LiveRoomModel.Data> {

        @Override
        int getType() {
            return VIEW_TYPE_LIVE;
        }
    }

    private class MediaDataHolder extends DataHolder<List<MediaModel.Data>> {

        @Override
        int getType() {
            return VIEW_TYPE_MEDIA;
        }
    }

    private class OfficeDataHolder extends DataHolder<OfficeModel.Data> {

        @Override
        int getType() {
            return VIEW_TYPE_OFFICE;
        }
    }

    private class OfficeGoodsDataHolder extends DataHolder<List<OfficeModel.Data>> {

        @Override
        int getType() {
            return VIEW_TYPE_OFFICE_GOODS;
        }
    }

    private class MoreDataHolder extends DataHolder<Integer> {

        @Override
        int getType() {
            return VIEW_TYPE_MORE;
        }
    }

    private class TagDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_TAG;
        }
    }

    private class TitleDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_TITLE;
        }
    }

    private class MediaTitleDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_MEDIA_TITLE;
        }
    }

    private class OfficeTitleDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_OFFICE_TITLE;
        }

    }

    private class QuestionDataHolder extends DataHolder {

        @Override
        int getType() {
            return VIEW_TYPE_QUESTION;
        }
    }

    private class TitleMenuDataHolder extends DataHolder {

        @Override
        int getType() {
            return VIEW_TYPE_TITLE_MENU;
        }
    }

    private class EventDataHolder extends DataHolder<List<MediaModel.Data>> {

        @Override
        int getType() {
            return VIEW_TYPE_EVENT;
        }
    }

    private class LineDataHolder extends DataHolder {

        @Override
        int getType() {
            return VIEW_TYPE_LINE;
        }
    }

    public void clear() {
        if (coachList != null) {
            coachList.clear();
            coachList = null;
        }
        if (answerList != null) {
            answerList.clear();
            answerList = null;
        }
        //liveData = null;
        interviewData = null;
        if (mediaList != null) {
            mediaList.clear();
            mediaList = null;
        }
        if (classMediaList != null) {
            classMediaList.clear();
            classMediaList = null;
        }
        if (officeList != null) {
            officeList.clear();
            officeList = null;
        }
        if (goodsList != null) {
            goodsList.clear();
            goodsList = null;
        }
        if (dataHolders != null) {
            dataHolders.clear();
            dataHolders = null;
        }
        if (mediaPlayerUtil != null) {
            mediaPlayerUtil.clearMediaPlayer();
        }

        if (handler != null) {
            handler.sendEmptyMessage(2);
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
        if (media != null) {
            media.pause();
            media.stop();
            media.release();
            media = null;
        }
    }
}
